<?php
namespace app\admin\controller;

class Com extends Base
{
    //通用编辑
    public function edit(){
        if(is_post()){
            $d = $this->post;
            if(!in_array($d['field'], ['sort'])){
                err('非通用字段!');
            }
            $this->db->update($d['table'],['id'=>$d['id'],$d['field']=>$d['value']]);
            cookie('js_msg','更新成功');
            cookie('js_function','alert|reload');
            return suc();
        }
    }

    //通用 拷贝一条数据
    function copys(){
        if(is_post()){
            $d = $this->post;
            $table = $d['table'];
            $id = (int)$d['id'];
            $temp = [
                "slide",
                "link",
                "goods",
                "img",
                "article",
            ];
            if(!in_array($table, $temp)){
                err('不可复制');
            }
            $info = $this->db->find($table,$id);
            if($info){
                unset($info['id']);
                unset($info['delete_time']);
                $info['create_time'] = time();
                $info['update_time'] = time();
                $info['title'] .= ' 拷贝';
                $id = $this->db->insert($table,$info);
                m('Attachment','xcom')->imgCopy($table,$id,$info);
                cookie('js_msg','拷贝成功');
                cookie('js_function','reload');
                return suc();
            }
            return err('无数据');
        }
    }
}
